package ui;

import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;

import javax.swing.JComboBox;
import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Minute;

import common.charts.WindDataSet;
import common.log.WindLog;
import common.wind.WindData;

import dataset.DataSetInterface;

public class MainFrame extends JFrame {

	//public static final int TODAY = 0;
	//public static final int YESTARDAY = 1;
	//public static final int DAY_BEFORE_YESTARDAY = 2;
	
	private javax.swing.JPanel jContentPane = null;
	private JComboBox dayOfwind = null;
	String[] dayStrings = { "OGGI", "IERI" };
	private ChartPanel chartPanel = null;
	private DataSetInterface ed = null;

	private void initialize(DataSetInterface aDataSet) {
//		this.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
		this.setDefaultCloseOperation(javax.swing.JFrame.HIDE_ON_CLOSE);
//		this.setDefaultCloseOperation(javax.swing.JFrame.);
		this.setResizable(false);
		this.setTitle("WindDisplay");
		this.setContentPane(getJContentPane());
		this.getContentPane().add(getDayOfwind(), BorderLayout.PAGE_START);
		this.getContentPane().add(getChartPanel(), BorderLayout.CENTER);
		try {
			ed=aDataSet;
			this.displayChart(dayOfwind.getSelectedIndex(), ed.getWindData());
		} catch (Exception e) {
			WindLog.getLogger().logSevere("Error retrieving data: " + e.getMessage());
		}
		this.setLocationRelativeTo(null);
	}

	public MainFrame(DataSetInterface aDataSet) throws HeadlessException {
		super();
		initialize(aDataSet);
	}

	private javax.swing.JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(new BorderLayout());
		}
		return jContentPane;
	}

	private JComboBox getDayOfwind() {
		if (dayOfwind == null) {
			dayOfwind = new JComboBox(dayStrings);
			dayOfwind.setSelectedIndex(0);
			dayOfwind.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
						displayChart(dayOfwind.getSelectedIndex(), ed
								.getWindData());
				}
			});
		}
		return dayOfwind;
	}

	private void displayChart(int aDay, ArrayList<WindData> ar){
		Iterator it = ar.iterator();
		WindDataSet eods = new WindDataSet();
		Calendar today = Calendar.getInstance();
		while (it.hasNext()) {
			WindData wd = (WindData) it.next();
			if (wd.getWindDateTime().getDayOfYear() == today
					.get(Calendar.DAY_OF_YEAR)
					- aDay) {
				Minute minute = new Minute(wd.getWindDateTime().getMin(), wd
						.getWindDateTime().getHour(), wd.getWindDateTime()
						.getDay(), wd.getWindDateTime().getMonth(), wd
						.getWindDateTime().getYear());
				eods.getAvgWind().addOrUpdate(minute, wd.getAvgspeed());
				eods.getCurrWind().addOrUpdate(minute, wd.getSpeed());
				eods.getMaxWind().addOrUpdate(minute, wd.getMaxspeed());
			}
		}

		JFreeChart chart = ChartFactory.createTimeSeriesChart(
				"Vento @ Civitanova", // chart title
				"Ore", // x axis label
				"Nodi", // y axis label
				eods, // data
				true, // include legend
				true, // tooltips
				false // urls
				);

		XYPlot plot = (XYPlot) chart.getPlot();
		XYItemRenderer r = plot.getRenderer();
		if (r instanceof XYLineAndShapeRenderer) {
			XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
			renderer.setBaseShapesVisible(true);
			renderer.setBaseShapesFilled(true);
		}
		DateAxis axis = (DateAxis) plot.getDomainAxis();
		axis.setDateFormatOverride(new SimpleDateFormat("HH-mm"));
		getChartPanel().setChart(chart);
	}

	private ChartPanel getChartPanel() {
		if (chartPanel == null) {
			chartPanel = new ChartPanel(null);
		}
		return chartPanel;
	}

	public void refeshChart(){
		displayChart(dayOfwind.getSelectedIndex(), ed
				.getWindData());
	}
	
}
